#include "ProjectImageWall.h"

ProjectImageWall::ProjectImageWall(const std::string &name, CC::Scene *scene)
	: ProjectSandbox(name, scene)
{
	CC_INFO("-----ProjectImageWall----\n");
}

ProjectImageWall::~ProjectImageWall()
{
}

void ProjectImageWall::OnAttach()
{
    ProjectSandbox::OnAttach();

    //GetAnimObj()->SetAnimTime(10000.0);

    for (int i = 0; i < MyImgMAXNUM; ++i) {
        
        std::string name = "Screenshot_" + CC::Tool::Int2Str(i + 1) + ".jpg";
        //CC_INFO("name=%s", name.c_str());
        m_imgs[i] = GetScene()->AddImageNode(GetScene()->GetRoot(), PATHCONFIG("assets/textures/" + name), name);
        m_imgs[i]->GetComponent<CC::TransformComponent>(CC::ComponentType_Transform)->Scale = glm::vec3(0.0, 0.0, 0.0);
        m_imgs[i]->GetComponent<CC::TransformComponent>(CC::ComponentType_Transform)->Translation = glm::vec3(0.0, 10000.0, 1.0);

        m_values[i] = 10000.0;

        m_anims[i] = new CC::AnimationPropertyGroup();

        if (i <= 4) {
            //level 1
            CC::AnimationPropertyEaseOutSine* anim = new CC::AnimationPropertyEaseOutSine(&m_values[i], 0.0, 0.0, 4000.0);
            m_anims[i]->AddAnimationProperty(anim);
            m_anims[i]->AddAnimationProperty(new CC::AnimationPropertyPause(2000.0));

            CC::AnimationPropertyEaseOutSine* anim2 = new CC::AnimationPropertyEaseOutSine(&m_values[i], 0.0, 0.0, 4000.0);
            m_anims[i]->AddAnimationProperty(anim2);
            m_anims[i]->AddAnimationProperty(new CC::AnimationPropertyPause(10000.0));
        }
        else {
            //level 2
            m_anims[i]->AddAnimationProperty(new CC::AnimationPropertyPause(10000.0));
            CC::AnimationPropertyEaseOutSine* anim = new CC::AnimationPropertyEaseOutSine(&m_values[i], 0.0, 0.0, 4000.0);
            m_anims[i]->AddAnimationProperty(anim);
            m_anims[i]->AddAnimationProperty(new CC::AnimationPropertyPause(2000.0));

            CC::AnimationPropertyEaseOutSine* anim2 = new CC::AnimationPropertyEaseOutSine(&m_values[i], 0.0, 0.0, 4000.0);
            m_anims[i]->AddAnimationProperty(anim2);
        }

        m_anims[i]->SetRepeat(true);
        GetScene()->AddAnimationPropertyGroup(m_anims[i]);

    }

    {
        m_txt_fps = GetScene()->AddTextFpsNode(GetScene()->GetRoot(), "fps");
        auto m = m_txt_fps->GetComponent<CC::Material2DComponent>(CC::ComponentType_Material2D);
        m->Color = glm::vec4(1.0f, 0.0f, 0.0f, 1.0f);
    }
   
}

void ProjectImageWall::OnDetach()
{
    ProjectSandbox::OnDetach();
}

void ProjectImageWall::OnUpdate(CC::Timestep ts, std::string renderscene)
{
    //CC_INFO("ProjectImageWall=%f", ts.GetMilliseconds());

    if (0 == GetShowWin()) {
        return;
    }

    ProjectSandbox::OnUpdate(ts, renderscene);

    auto tran = GetNode()->GetComponent<CC::TransformComponent>(CC::ComponentType_Transform);
    tran->Translation = glm::vec3(0.0, 0.0, 0.0);
    tran->Scale = glm::vec3(GetWidth(), GetHeight(), 0.0);

    static int startX = 0;
    static int oneBlockWidth = 0;
    static int imgWidthScale = 0;

    if (GetHeight() > 0.0) {
        if (!m_init) {
            m_init = true;

            const float imgWidth = 1080.0;
            const float imgHeight = 2110.0;
            float ratio = GetHeight() / imgHeight;
            imgWidthScale = ratio * imgWidth; //keep height=GetHeight, so width scale

            oneBlockWidth = GetWidth() / (float)m_imgNumInScreen;

            if (oneBlockWidth >=  imgWidthScale) {                
                startX = (oneBlockWidth - imgWidthScale) / 2.0;
            }
            else {
                imgWidthScale = oneBlockWidth;
            }

            for (int i = 0; i < MyImgMAXNUM; ++i) {
                if (!m_imgs[i]) break;
                m_imgs[i]->GetComponent<CC::TransformComponent>(CC::ComponentType_Transform)->Scale = glm::vec3((float)imgWidthScale, GetHeight(), 0.0);

                auto tran = m_imgs[i]->GetComponent<CC::TransformComponent>(CC::ComponentType_Transform);
                tran->Translation[0] = (i % m_imgNumInScreen) * imgWidthScale + startX;

                if (i <= 4) {
                    //level 1
                    int dir = i % 2 == 0 ? 1 : -1;
                    m_anims[i]->GetAnimationProperty(0)->SetStartValue(GetHeight() * dir);
                    m_anims[i]->GetAnimationProperty(2)->SetEndValue(-GetHeight() * dir);
                }
                else {
                    //level 2
                    int dir = i % 2 == 0 ? 1 : -1;
                    m_anims[i]->GetAnimationProperty(1)->SetStartValue(-GetHeight() * dir);
                    m_anims[i]->GetAnimationProperty(3)->SetEndValue(GetHeight() * dir);
                }

                m_anims[i]->Start();
            } 
            
        }
    }

    for (int i = 0; i < MyImgMAXNUM; ++i) {
        if(!m_imgs[i]) break;
        auto tran2 = m_imgs[i]->GetComponent<CC::TransformComponent>(CC::ComponentType_Transform);

        int y = (int)m_values[i];
        tran2->Translation[1] = (float)y;
    }
    
}